package com.qijian.common.utils;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.Base64;

public class EAMicUtil {

    private final static String WORKLIST_URL = "https://valueapex.com/eamic/ws/rest/v1/woList";

    public static String main(String[] args) throws IOException {
        String body = "";
        CloseableHttpClient client = HttpClients.createDefault();
        String encoding = Base64.getEncoder().encodeToString(("00074:JZNee20220823value").getBytes());
        HttpUriRequest httpUriRequest = RequestBuilder.get(WORKLIST_URL+"?nb_items=99999999").
                setHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded").
                addHeader("Authorization", "Basic " + encoding).build();
        //执行请求操作，并拿到结果（同步阻塞）
        CloseableHttpResponse response = client.execute(httpUriRequest);
        //获取结果实体
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            //按指定编码转换结果实体为String类型
            body = EntityUtils.toString(entity, "UTF-8");
        }
        EntityUtils.consume(entity);
        //释放链接
        response.close();
        System.out.println("body: " + body);
        return body;
    }

    public static String getAllWoList() throws IOException {
        String body = "";
        CloseableHttpClient client = HttpClients.createDefault();
        String encoding = Base64.getEncoder().encodeToString(("00074:JZNee20220823value").getBytes());
        HttpUriRequest httpUriRequest = RequestBuilder.get(WORKLIST_URL+"?nb_items=99999999").
                setHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded").
                addHeader("Authorization", "Basic " + encoding).build();
        //执行请求操作，并拿到结果（同步阻塞）
        CloseableHttpResponse response = client.execute(httpUriRequest);
        //获取结果实体
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            //按指定编码转换结果实体为String类型
            body = EntityUtils.toString(entity, "UTF-8");
        }
        EntityUtils.consume(entity);
        //释放链接
        response.close();
        return body;
    }

    private final static String HISTORY_WORKLIST_URL = "https://valueapex.com/eamic/ws/rest/v1/woHistory";

    public static String getHistoryWoList(Integer nb_items,Integer page) throws IOException {
        String body = "";
        CloseableHttpClient client = HttpClients.createDefault();
        String encoding = Base64.getEncoder().encodeToString(("00074:JZNee20220823value").getBytes());
        HttpUriRequest httpUriRequest = RequestBuilder.get(HISTORY_WORKLIST_URL+"?nb_items="+nb_items+"&page="+page).
                setHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded").
                addHeader("Authorization", "Basic " + encoding).build();
        //执行请求操作，并拿到结果（同步阻塞）
        CloseableHttpResponse response = client.execute(httpUriRequest);
        //获取结果实体
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            //按指定编码转换结果实体为String类型
            body = EntityUtils.toString(entity, "UTF-8");
        }
        EntityUtils.consume(entity);
        //释放链接
        response.close();
        return body;
    }

}
